module StaticPagesControllerActions
  extend ActiveSupport::Concern

  included do
    helper_method :static_page_path, :edit_static_page_path
  end

  def show
    @static_page = StaticPage.find_by!(slug: params[:controller])
    render 'static_pages/show'
  end

  def edit
    @static_page = StaticPage.find_by!(slug: params[:controller])
    render 'static_pages/edit'
  end

  def update
    @static_page = StaticPage.find_by!(slug: params[:controller])
    if @static_page.update_attributes(static_page_params)
      redirect_to static_page_path
    else
      render 'static_pages/edit'
    end
  end

  private

  def static_page_params
    params.require(:static_page).permit(:slug, :title, :content)
  end
end
